#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>

int main(int args, const char * argv[]){
	
	if(args < 3){
		perror("less args");
		exit(0);
	}

	int sockfd;
	struct sockaddr_in addr;
	char buf[BUFSIZ] = "chengaj";  
	//1. 创建套接字：IPV4、TCP
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sockfd < 0){
		perror("socket");
		exit(0);
	}

	//2. 向服务端发送客户端请求
	addr.sin_family = AF_INET;
	addr.sin_port = htons(atoi(argv[2]));
#if 1
	if (inet_aton(argv[1], &addr.sin_addr) == 0) {
		fprintf(stderr, "Invalid address\n");
		exit(EXIT_FAILURE);
	}
#else
	addr.sin_addr.s_addr = inet_addr(argv[1]);
#endif
	if(connect(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) == -1){
		perror("connect");
		exit(0);
	}

	//3. 发送数据
	while(1){
		printf("Input->");
		fgets(buf, BUFSIZ, stdin);
		write(sockfd, buf, strlen(buf));
	}

	//4. 关闭套接字
	close(sockfd);
}
